﻿package {
	import flash.display.MovieClip;
	import flash.text.*;
	import flash.utils.*;
	import flash.events.*;
	
	public class MusicPlayerBar extends MovieClip {
		
		private var box:MPBox = new MPBox();
		private var bar:MPBar = new MPBar();
		private var totTime:Number;
		private var currentTime:Number = 0;
		private var finish:Function;
		private var once:Boolean = false;
		private var titel:TextField = new TextField();
		private var timer:Timer;
		
		public function MusicPlayerBar(artist:String, album:String) {
			this.totTime = 1;
			//visible = false;
			box.y = bar.y - (box.height / 2) + (bar.height / 2);
			update(0);
			
			titel.width = bar.width;
			titel.x = bar.x;
			titel.y = 20;
			
			var field:TextField = new TextField();
			field.text = artist + " - " + album;
			//field.autoSize = TextFieldAutoSize.LEFT;
			field.x = bar.x; //+ (bar.width - field.width) / 2;
			field.width = bar.width;
			field.y = -40;
			field.setTextFormat(getFormat(20, true));
			
			addChild(titel);
			addChild(field);
			addChild(bar);
			addChild(box);
		}
		
		public function init() {
			var initY:Number = y;
			y = - 50;
			//visible = true;
			var id:Number = setInterval(moveme, 4);
			//var i:int = 0;
			function moveme() {
				if(y >= initY) {
					clearInterval(id);
					return;
				}
					
				y = y + 2;
			}
		}
		
		public function destroy() {
			var i:int = 0;
			var id:Number = setInterval(moveme, 20);
			function moveme() {
				if(i == 20)
					clearInterval(id);
				
				y = y - 5;
				i++;
			}
		}
		
		public function setEndListener(listener:Function) {
			finish = listener;
		}
		
		public function playNew(titel:String, totTime:Number) { //totTime = tid i s
			this.totTime = totTime;
			this.titel.text = "Playing: " + titel;
			this.titel.setTextFormat(getFormat(15, false));
			reset();
			timer = new Timer(1000, totTime + 1);
			//this.inc = bar.width / totTime;
			timer.addEventListener(TimerEvent.TIMER,
				function(event2:TimerEvent):void{
					tick();
				});
			timer.start();			
		}
		
		public function stopSlider() {
			timer.stop();
		}
		
		public function reset() {
			currentTime = 0;
			if(timer != null)
				timer.stop();
			once = false;
			update(0);
		}
		
		public function setTime(time:Number) {
			if(time >= totTime) {
				update(totTime);
				if(!once && finish != null)
					finish();
			}
			else if(time <= 0)
				update(0);
			else
				update(time);
		}
		
		public function tick() {
			if(currentTime >= totTime) {
				trace("Slider has finished!");
				if(finish != null && !once) {
					trace("Shouting: finished!");
					once = true;
					finish();
				}
					
				return;
			}
				
			currentTime++;
			update(currentTime);
		}
		
		private function update(time:Number) {
			var len:Number = (time / totTime) * bar.width;
			box.x = len - (box.width / 2) + bar.x;
		}
		
		private function getFormat(size:int, bold:Boolean) {
			var txt:TextFormat = new TextFormat();
			txt.align = "center";
			txt.bold = bold;
			txt.color = 0x2F10AB;
			txt.size = size;
			txt.font = "Trebuchet MS";
			return txt;
		}
	}
}